package com.wf.course.web.controller.course;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.utils.page.PageBean;
import com.wf.course.model.course.Speaker;
import com.wf.course.service.ISpeakerService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 主讲人章节列表Controller
 */
@RequestMapping("speaker")
@Controller
public class SpeakerController {

	@Autowired
	private ISpeakerService speakerService;

	/**
	 * 讲师列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param speakerName
	 * @param status
	 * @param model
	 * @return
	 */
	@LogInfo("查询主讲人信息列表")
	@RequestMapping("speakerList")
	public String chapterList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "speakerName", required = false) String speakerName, 
			@RequestParam(value = "status", required = false) String status, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("speakerName", speakerName);
		cons.put("status", status);
		PageBean<Speaker> pageBean = new PageBean<Speaker>(pageNo, pageSize, null, cons);
		pageBean.setUrl("speaker/speakerList");
		this.speakerService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		return "course/speakerList";
	}

	/**
	 * 主讲人 修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增/修改主讲人信息，页面数据初始化")
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			Speaker speaker = speakerService.querySpeakerById(id);
			model.addAttribute("speaker", speaker);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		
		return "course/speakerEdit";
	}

	/**
	 * 主讲人新增和修改
	 * @param Speaker
	 * @param imgFile
	 * @param h5File
	 * @param request
	 * @return
	 */
	@LogInfo("新增或修改主讲人信息")
	@RequestMapping(value = "speakerEdit" , produces = "text/html;charset=UTF-8")
	public String speakerEdit(@ModelAttribute Speaker Speaker,MultipartFile imageFile, HttpServletRequest request) {
		
		speakerService.saveOrUpdateSpeaker(Speaker,imageFile);
		
		return "redirect:/speaker/speakerList";
	}

	/**
	 * 主讲人删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除主讲人信息")
	@RequestMapping(value = "remove/{id}")
	public String removeSpeaker(@PathVariable("id") Integer id) {
		
		speakerService.deleteById(id);
		
		return "redirect:/speaker/speakerList";
	}
	

}
